package com.zhufei.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import antlr.HTMLCodeGenerator;

import com.zhufei.entity.AboutUs;
import com.zhufei.entity.Video;

public class VideoDao extends DBCon{
	private static String TABLE = "zhufei_video";
	private static String ID = "ID";
	private static String HTML_CODE = "html_code";
	private static String NAME = "video_name";
	private static String TIME = "time";
	private static String SHOW = "is_show";
	
	/**
	 * 插入一条视频记录到数据库
	 * @param html_code  html代码
	 * @param name  视频名字
	 * @param show  是否显示视频，1表示显示，0表示不显示
	 * @return
	 */
	public static boolean insertVideo(String html_code, String name, int show){
		DBCon con = new DBCon();
		try {
			String id = GlobalsDao.getUUID();
			String dt = GlobalsDao.getCurrentDateTime();
			Statement statement = con.getStatement();
			String sql = "insert into " + TABLE + " ("+ID+","+HTML_CODE+","+NAME+","+TIME+","+SHOW+") values ('"+id+"','"+html_code+"','"+name+"','"+dt+"',"+show+")";
			statement.execute(sql);
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			con.closeDB();
		}
		return false;
	}
	
	/**
	 * 获取数据库中唯一一条视频记录
	 * @return
	 */
	public static Video getVideo(){
		DBCon con = new DBCon();
		try {
			Statement statement = con.getStatement();
			String sql = "select * from "+TABLE+" limit 0,1";
			ResultSet rs = statement.executeQuery(sql);
			if(rs.next()){
				Video a = new Video();
				a.setID(rs.getString(ID));
				a.setHtml(rs.getString(HTML_CODE));
				a.setName(rs.getString(NAME));
				a.setDatetime(rs.getString(TIME));
				a.setShow(rs.getInt(SHOW));
				rs.close();
				return a;
			}else{
				rs.close();
				return null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			con.closeDB();
		}
		return null;
	}
	
	/**
	 * 修改视频对象
	 * @param id   id
	 * @param html_code  html代码
	 * @param name   视频名字
	 * @param show   是否显示视频，1表示显示，0表示不显示
	 * @return
	 */
	public static boolean updateVideo(String html_code, String name, int show){
		DBCon con = new DBCon();
		try {
			String dt = GlobalsDao.getCurrentDateTime();
			
			Statement statement = con.getStatement();
			
			html_code = html_code.replace("480", "197");
			html_code = html_code.replace("400", "181");
			String sql = "update "+TABLE+" set "+HTML_CODE+"='"+html_code+"',"+NAME+"='"+name+"',"+TIME+"='"+dt+"',"+SHOW+"="+show+" where "+NAME+"='前台视频'";
			statement.executeUpdate(sql);
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			con.closeDB();
		}
		return false;
	}
	
	/**
	 * 删除
	 * @param ID      视频ID
	 * @return  true表示成功，false表示失败
	 */
	public static boolean deleteVideo(String ID){
		DBCon con = new DBCon();
		try {
			Statement statement = con.getStatement();
			String sql = "delete from "+TABLE+" where ID ='"+ID+"'";
			statement.executeUpdate(sql);
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			con.closeDB();
		}
		return false;
	}
}
